package hot100.h0911;

import h0904.TwoNumAdd;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description 相交链表
 * @date 2024/9/11 22:51
 */
public class ReturnCommonNode {
    public static void main(String[] args) {

    }

    static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) {
            return null;
        }
        ListNode pa = headA;
        ListNode pb = headB;

        while (pa != pb) {
            // 如果指针A到达链表的末尾，则将其重定位到链表B的头部
            pa = pa != null ? pa.next : headB;
            // 如果指针B到达链表的末尾，则将其重定位到链表A的头部
            pb = pb != null ? pb.next : headA;
        }

        return pa;
    }
}
